# -*- coding: utf-8 -*-

from MyLibrary import *

# 地牢
class Dungeon():
    def __init__(self):
        # 加载精灵
        self.text = MySprite()
        self.text.load("ascii8x12.png", 8, 12, 32)

        # 创建地图
        self.tiles = list()
        for n in range(0, 80*45):   # 80行 45列
            self.tiles.append(-1)

        # self.generate()

    def getCharAt(self, x, y):
        if x < 0 or x > 79 or y < 0 or y > 44:
            print('error: x,y = ', x, y)
            return
        index = y * 80 + x
        if index < 0 or index > 80*45:
            print('error: index = ', index)
            return
        return self.tiles[index]

    def setCharAt(self, x, y, char):
        if x < 0 or x > 79 or y < 0 or y > 44:
            print('error: x,y = ', x, y)
            return
        index = y * 80 + x
        if index < 0 or index > 80*45:
            print('error: index = ', index)
            return
        self.tiles[index] = char

    def draw(self, surface, offx, offy):
        for y in range(0, 45):
            for x in range(0, 80):
                value = self.getCharAt(x, y)
                if value >= 0:
                    self.text.X = offx + x * 8
                    self.text.Y = offy + y * 12
                    self.text.frame = value
                    self.text.last_frame = value
                    self.text.update(0)
                    self.text.draw(surface)

    def createRoom(self, x, y, rposx, rposy, rsizel, rsizeh):
        room = Rect(x + random.randint(1, rposx),
                    y + random.randint(1, rposy),
                    random.randint(rsizel, rsizeh),
                    random.randint(rsizel, rsizeh))
        self.rooms.append(room)

    def generate(self, emptyChar=176, roomChar=219, hallChar=178):
        self.roomChar = roomChar
        self.hallChar = hallChar
        self.emptyChar = emptyChar
        # clear
        for index in range(0, 80*45):
            self.tiles[index] = emptyChar

        # 创建随机房间
        self.rooms = list()
        PL = 4
        PH = 8
        SL = 5
        SH = 14
        # 北面房间
        self.createRoom(0,0,PL,PH,SL,SH)
        self.createRoom(20,0,PL,PH,SL,SH)
        self.createRoom(40,0,PL,PH,SL,SH)
        self.createRoom(60,0,PL,PH,SL,SH)

        # 南面房间
        self.createRoom(0, 22, PL, PH, SL, SH)
        self.createRoom(20, 22, PL, PH, SL, SH)
        self.createRoom(40, 22, PL, PH, SL, SH)
        self.createRoom(60, 22, PL, PH, SL, SH)

        # 创建走廊
        self.createHallRight(self.rooms[0], self.rooms[1], hallChar)
        self.createHallRight(self.rooms[1], self.rooms[2], hallChar)
        self.createHallRight(self.rooms[2], self.rooms[3], hallChar)
        self.createHallRight(self.rooms[4], self.rooms[5], hallChar)
        self.createHallRight(self.rooms[5], self.rooms[6], hallChar)
        self.createHallRight(self.rooms[6], self.rooms[7], hallChar)

        choice = random.randint(0, 3)
        self.createHallDown(self.rooms[choice], self.rooms[choice+4], hallChar)

        # 将房间添加到关卡
        for room in self.rooms:
            for y in range(room.y, room.y+room.height):
                for x in range(room.x, room.x+room.width):
                    self.setCharAt(x, y, roomChar)

        # 创建入口/出口
        choice = random.randint(0, 7)
        self.entrance_x = self.rooms[choice].x + self.rooms[choice].width // 2
        self.entrance_y = self.rooms[choice].y + self.rooms[choice].height // 2
        self.setCharAt(self.entrance_x, self.entrance_y, 30)  # 入口
        # print('enter: ', choice, self.entrance_x, self.entrance_y)

        choice2 = random.randint(0, 7)
        while choice2 == choice:
            choice2 = random.randint(0, 7)
        x = self.rooms[choice2].x + self.rooms[choice2].width // 2
        y = self.rooms[choice2].y + self.rooms[choice2].height // 2
        self.setCharAt(x, y, 31)  # 出口
        # print('exit: ', choice, x, y)

        # 创建金子
        drops = random.randint(5, 20)
        for n in range(1, drops):
            self.putCharInRandomRoom(roomChar, 71)

        # 创建武器, 盔甲, 生命
        self.putCharInRandomRoom(roomChar, 87)  # w
        self.putCharInRandomRoom(roomChar, 65)  # a
        self.putCharInRandomRoom(roomChar, 72)  # h
        self.putCharInRandomRoom(roomChar, 72)  # h

        # 创建怪兽
        num = random.randint(5, 10)
        for n in range(0, num):
            self.putCharInRandomRoom(roomChar, 137)

    # 放置物品
    def putCharInRandomRoom(self, targetChar, itemChar):
        tile = 0
        while tile != targetChar:
            x = random.randint(0, 79)
            y = random.randint(0, 44)
            tile = self.getCharAt(x, y)
        self.setCharAt(x, y, itemChar)

    # 水平通道
    def createHallRight(self, src, dst, hallChar):
        # 左边房间连接右侧房间
        pathx = src.x + src.width
        pathy = src.y + random.randint(1, src.height-2)
        self.setCharAt(pathx, pathy, hallChar)

        # 起点y在右侧房间高度范围内
        if pathy > dst.y and pathy < dst.y + dst.height:
            while pathx < dst.x:
                pathx += 1
                self.setCharAt(pathx, pathy, hallChar)  # 直线连接右侧房间
        else:
            while pathx < dst.x + 1:
                pathx += 1
                self.setCharAt(pathx, pathy, hallChar)
                # 右侧房间偏下
                if pathy < dst.y + 1:
                    self.setCharAt(pathx, pathy, hallChar)
                    while pathy < dst.y:
                        pathy += 1
                        self.setCharAt(pathx, pathy, hallChar)  # 垂直向下
                else:
                    # 右侧房间偏上
                    # self.setCharAt(pathx, pathy, hallChar)
                    while pathy > dst.y + dst.height:
                        pathy -= 1
                        self.setCharAt(pathx, pathy, hallChar)  # 垂直向上

    # 垂直通道
    def createHallDown(self, src, dst, hallChar):
        # 上边房间连接下边房间
        pathx = src.x + random.randint(1, src.width-2)
        pathy = src.y + src.height
        self.setCharAt(pathx, pathy, hallChar)

        # 起点x在下面房间宽度范围内
        if pathx > dst.x and pathx < dst.x + dst.width:
            while pathy < dst.y:
                pathy += 1
                self.setCharAt(pathx, pathy, hallChar)  # 直线连接右侧房间
        else:
            while pathy < dst.y + 1:
                pathy += 1
                self.setCharAt(pathx, pathy, hallChar)
                # 下方房间偏右
                if pathx < dst.x + 1:
                    self.setCharAt(pathx, pathy, hallChar)
                    while pathx < dst.x:
                        pathx += 1
                        self.setCharAt(pathx, pathy, hallChar)  # 垂直向下
                else:
                    # 下方房间偏左
                    # self.setCharAt(pathx, pathy, hallChar)
                    while pathx > dst.x + dst.width:
                        pathx -= 1
                        self.setCharAt(pathx, pathy, hallChar)  # 垂直向上

    # 绘制玩家
    def draw_char(self, surface, x, y, char):
        self.text.X = 30 + x * 8
        self.text.Y = 30 + y * 12
        self.text.frame = char
        self.text.last_frame = char
        self.text.update(0)
        self.text.draw(surface)
